﻿using System.Text;
using System.Runtime.InteropServices;

namespace BellAssistant.Common.CommonHelper
{
    public class INIFileHelper
    {
        public string inipath;   //INI文件名
        //调用动态链接库
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        //声明读写INI文件的API函数     
        public INIFileHelper(string INIPath)
        {
            inipath = INIPath;
        }

        /*
        * Section表示.ini文件所在项目名称
        * Key表示键Key的名称
        * Value表示键Key的值         
        */
        //类的构造函数，传递INI文件名
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }

        //读INI文件         
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
            return temp.ToString();
        }
    }
}
